import { defineStore } from 'pinia';
import { Version } from 'src/services/type';
import { listVersions } from 'src/services/versionService';

export const useVersionStore = defineStore('useVersionStore', {
  state: () => ({
    version: [] as Version[],
    versionMap: new Map<string, { hw_ver: string[]; sw_ver: string[] }>(),
  }),
  getters: {},
  actions: {
    async init() {
      this.versionMap.clear()
      const data = await listVersions()
      if (data) {
        this.version = data;
        for (const d of data) {
          const currentEntry = this.versionMap.get(d.stock_code) ?? { hw_ver: [], sw_ver: [] };
          if (currentEntry.hw_ver.indexOf(d.hw_ver.replace(/ /g, ''))==-1 ){
            currentEntry.hw_ver.unshift(d.hw_ver.replace(/ /g, ''));
          }
          if (currentEntry.sw_ver.indexOf(d.sw_ver.replace(/ /g, ''))==-1 ){
            currentEntry.sw_ver.unshift(d.sw_ver.replace(/ /g, ''));
          }
          this.versionMap.set(d.stock_code, currentEntry);
        }
      }
    },
    checkSWVersion(stock_code: string, sw_ver: string) {
      if (this.versionMap.has(stock_code)) {
        const latest = this.versionMap.get(stock_code);
        const sw_list = sw_ver.trim().split(',')
        if (latest&&sw_list.length>1){    
          for (const sw of sw_list){
            if (latest?.sw_ver.includes('')) continue
            return latest?.sw_ver.includes(sw.replace(/ /g, ''))
          }
        }
        
        return latest?.sw_ver.includes(sw_list[0]);
      }
      return false;
    },
    checkHWVersion(stock_code: string, hw_ver: string) {
      if (this.versionMap.has(stock_code)) {
        const latest = this.versionMap.get(stock_code);
        if (latest&&hw_ver){
          const hw_list = hw_ver.trim().split(',')
          for (const hw of hw_list){
            if (latest?.hw_ver.includes('')) continue
            if (latest?.hw_ver.includes(hw.replace(/ /g, ''))) return true
          }
          return latest?.hw_ver.includes(hw_ver)
        }
      }
      return true;
    },
    getswVer(stock_code: string) {
      if (this.versionMap.has(stock_code)) {
        const latest = this.versionMap.get(stock_code);     
        if (latest){
          const show = []
          for(const d of latest.sw_ver){
            if (d=='') continue
              show.push(d)
          }
          return show;
        }   
      }
      return ['']
    },
    gethwVer(stock_code: string) {
      if (this.versionMap.has(stock_code)) {
        const latest = this.versionMap.get(stock_code);
        if (latest){
          const show = []
          for(const d of latest.hw_ver){
            if (d=='') continue
              show.push(d)
          }
          return show;
        }   
      }
      return ['']
    },
    getStatusClass(stock_code: string, sw_ver: string, hw_ver: string){
      const sw = this.checkSWVersion(stock_code, sw_ver);
      const hw = this.checkHWVersion(stock_code, hw_ver);
      if (sw && hw) {
        return 'version-pass';
      }
      return 'version-fail';
    },
    getVersionStatusClass(stock_code: string, sw_ver: string, hw_ver: string){
      const sw = this.checkSWVersion(stock_code, sw_ver);
      const hw = this.checkHWVersion(stock_code, hw_ver);
      if (sw && hw) {
        return 'pass';
      }
      if(sw){
        return 'hardware-version-fail'
      }
      if(hw){
        return 'software-version-fail'
      }
      return 'version-fail';
    },
    getSwStatusClass(stock_code: string,sw_vers: string){
      let sw_result = 'fail'
      const sw_list = sw_vers.trim().split(',')
      console.log(stock_code, sw_vers);
      
      if (this.versionMap.has(stock_code)) {
        const latest = this.versionMap.get(stock_code);
        for (const sw_ver of sw_list){
          // if (sw_ver=='') continue
          if (latest?.sw_ver.indexOf(sw_ver.replace(/ /g, ''))!= -1) {
            sw_result = 'pass'
            
          }
        }
      }
     return sw_result
      
    },
    getHwStatusClass(stock_code: string,hw_vers: string){
      let hw_result = 'fail'
      const hw_list = hw_vers.trim().split(',')
      
      if (this.versionMap.has(stock_code)) {
        const latest = this.versionMap.get(stock_code);
        for (const hw_ver of hw_list){
          // if (hw_ver=='') continue
          if (latest?.hw_ver.indexOf(hw_ver.replace(/ /g, ''))!= -1){
            hw_result = 'pass'
            
          }
        }
      }
      return hw_result

    }
  },
});
